RvRStats = {}

local window = "CharacterWindowRvRStats"
RvRStats.Window = window
local playername = WStringToString(GameData.Player.name)
local oldShow = nil
RvRStats.MESSAGES = {}
RvRStats.Messages = {}

---- Init ----
function RvRStats.Init()
   RvRStats.InitLocale()
   oldShow = CharacterWindow.OnShown
   CharacterWindow.OnShown = RvRStats.OnShown
end

function RvRStats.InitLocale()
	local switch = {
		[SystemData.Settings.Language.ENGLISH] = L"enUS",
		[SystemData.Settings.Language.FRENCH] = L"frFR",
		[SystemData.Settings.Language.GERMAN] = L"deDE",
		[SystemData.Settings.Language.ITALIAN] = L"itIT",
		[SystemData.Settings.Language.SPANISH] = L"esES",
		[SystemData.Settings.Language.RUSSIAN] = L"ruRU"
	}
		
	local res = switch[SystemData.Settings.Language.active] 
	if not res then
		res = L"enUS"
	end

    RvRStats.Messages = RvRStats.MESSAGES[res]
end

function RvRStats.GetMessage(key)
    return RvRStats.Messages[key]
end

function RvRStats.OnShown()
   RvRStats.UpdateWindow()
   oldShow()
end

local function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function RvRStats.UpdateWindow()
   local SafeDeaths = math.max(GameData.Player.RvRStats.LifetimeDeaths, 1)
   local KDRatio = (GameData.Player.RvRStats.LifetimeKills / SafeDeaths)
   local DBDRatio = (GameData.Player.RvRStats.LifetimeDeathBlows / SafeDeaths)
   LabelSetText(window.."Header", GameData.Player.name)
   LabelSetText(window.."Title", RvRStats.GetMessage("RR") .. L": " .. GameData.Player.Renown.curRank .. L"   " .. RvRStats.GetMessage("TITLE") .. L": " .. GameData.Player.Renown.curTitle)
   LabelSetText(window.."LifetimeK", RvRStats.GetMessage("LIFETIME_KILLS")..L": " .. (GameData.Player.RvRStats.LifetimeKills))
   LabelSetText(window.."LifetimeDB", RvRStats.GetMessage("LIFETIME_DEATHBLOWS")..L": " .. (GameData.Player.RvRStats.LifetimeDeathBlows))
   LabelSetText(window.."LifetimeD", RvRStats.GetMessage("LIFETIME_DEATHS")..L": " .. (GameData.Player.RvRStats.LifetimeDeaths))
   LabelSetText(window.."LifetimeRatioKD", RvRStats.GetMessage("LIFETIME_RATIO_KILL_DEATH")..(StringToWString(string.format(": %.2f" , KDRatio))))
   LabelSetText(window.."LifetimeRatioDBD", RvRStats.GetMessage("LIFETIME_RATIO_DEATHBLOW_DEATH")..(StringToWString(string.format(": %.2f" , DBDRatio))))
   LabelSetText(window.."SessionK", RvRStats.GetMessage("SESSION_KILLS")..L": " .. (GameData.Player.RvRStats.SessionKills))
   LabelSetText(window.."XPBonus", RvRStats.GetMessage("RVR_XP_BONUS")..L": " .. (GameData.Player.RvRStats.XPBonusBuff))
   LabelSetText(window.."RenownBonus", RvRStats.GetMessage("RVR_RENOWN_BONUS")..L": " .. (GameData.Player.RvRStats.RenownBonusBuff))
   LabelSetText(window.."ClassKills", RvRStats.GetMessage("CLASS_KILLS"))

   local race = GameData.Player.race.id
   local classA = 0
   local classB = 0
   local classC = 0
   local classD = 0
   local classE = 0
   local classF = 0
   local classG = 0
   local classH = 0
   local classI = 0
   local classJ = 0
   local classK = 0
   local classL = 0
   
   if race == 2 or race == 3 or race == 5 or race == 7 then
      local subTypeData = TomeGetBestiarySubTypeData( 16 ) --human
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 51 then classA = speciesData.killCount end --witch hunter
            if speciesData.id == 50 then classB = speciesData.killCount end --warrior priest
            if speciesData.id == 48 then classC = speciesData.killCount end --bright wizard
            if speciesData.id == 49 then classK = speciesData.killCount end --kotbs
         end
      end
      local subTypeData = TomeGetBestiarySubTypeData( 13 ) --dwarf
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 44 then classF = speciesData.killCount end --ironbreaker
            if speciesData.id == 45 then classG = speciesData.killCount end --runepriest
            if speciesData.id == 42 then classE = speciesData.killCount end --engineer
            if speciesData.id == 43 then classL = speciesData.killCount end --slayer (id = 46 in version 1.1.1)
         end
      end
      local subTypeData = TomeGetBestiarySubTypeData( 14 ) -- high elf
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 78 then classH = speciesData.killCount end --swordmaster
            if speciesData.id == 76 then classI = speciesData.killCount end --white lion
            if speciesData.id == 75 then classJ = speciesData.killCount end --archmage
            if speciesData.id == 77 then classD = speciesData.killCount end --shadow warrior
         end
      end
      LabelSetText(window.."ClassA", RvRStats.GetMessage("WITCH_HUNTER") .. L": " .. classA) 
      LabelSetText(window.."ClassB", RvRStats.GetMessage("WARRIOR_PRIEST") .. L": " .. classB)
      LabelSetText(window.."ClassC", RvRStats.GetMessage("BRIGHT_WIZARD") .. L": " .. classC)
      LabelSetText(window.."ClassD", RvRStats.GetMessage("KNIGHT_OF_THE_BLAZING_SUN") .. L": " .. classK)
      LabelSetText(window.."ClassE", RvRStats.GetMessage("IRONBREAKER") .. L": " .. classF)
      LabelSetText(window.."ClassF", RvRStats.GetMessage("RUNEPRIEST") .. L": " .. classG)
      LabelSetText(window.."ClassG", RvRStats.GetMessage("ENGINEER") .. L": " .. classE)
      LabelSetText(window.."ClassH", RvRStats.GetMessage("SLAYER") .. L": " .. classL)
      LabelSetText(window.."ClassI", RvRStats.GetMessage("SWORDMASTER") .. L": " .. classH)
      LabelSetText(window.."ClassJ", RvRStats.GetMessage("WHITE_LION") .. L": " .. classI)
      LabelSetText(window.."ClassK", RvRStats.GetMessage("ARCHMAGE") .. L": " .. classJ)
      LabelSetText(window.."ClassL", RvRStats.GetMessage("SHADOW_WARRIOR") .. L": " .. classD)
   end      
   if race == 1 or race == 4 or race == 6  then

      local subTypeData = TomeGetBestiarySubTypeData( 16 ) --human
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 14 then classA = speciesData.killCount end --chosen
            if speciesData.id == 16 then classB = speciesData.killCount end --marauder
            if speciesData.id == 17 then classC = speciesData.killCount end --zealot
            if speciesData.id == 15 then classD = speciesData.killCount end --magus
         end
      end
      local subTypeData = TomeGetBestiarySubTypeData( 15 ) --greenskin
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 63 then classF = speciesData.killCount end --shaman
            if speciesData.id == 64 then classG = speciesData.killCount end --squig herder
            if speciesData.id == 92 then classE = speciesData.killCount end --black orc
            if speciesData.id == 93 then classL = speciesData.killCount end --choppa
         end
      end
      local subTypeData = TomeGetBestiarySubTypeData( 14 ) -- dark elf
      if subTypeData then
         for index, speciesData in ipairs( subTypeData.species ) do
            if speciesData.id == 31 then classH = speciesData.killCount end --witch elf
            if speciesData.id == 32 then classI = speciesData.killCount end --disciple of khaine
            if speciesData.id == 33 then classK = speciesData.killCount end --blackguard
            if speciesData.id == 34 then classJ = speciesData.killCount end --sorcerer
         end
      end
      LabelSetText(window.."ClassA", RvRStats.GetMessage("CHOSEN") .. L": " .. classA)
      LabelSetText(window.."ClassB", RvRStats.GetMessage("MARAUDER") .. L": " .. classB)
      LabelSetText(window.."ClassC", RvRStats.GetMessage("ZEALOT") .. L": " .. classC)
      LabelSetText(window.."ClassD", RvRStats.GetMessage("MAGUS") .. L": " .. classD)
      LabelSetText(window.."ClassE", RvRStats.GetMessage("BLACK_ORC") .. L": " .. classE)
      LabelSetText(window.."ClassF", RvRStats.GetMessage("CHOPPA") .. L": " .. classL)
      LabelSetText(window.."ClassG", RvRStats.GetMessage("SHAMAN") .. L": " .. classF)
      LabelSetText(window.."ClassH", RvRStats.GetMessage("SQUIG_HERDER") .. L": " .. classG)
      LabelSetText(window.."ClassI", RvRStats.GetMessage("WITCH_ELF") .. L": " .. classH)
      LabelSetText(window.."ClassJ", RvRStats.GetMessage("DISCIPLE_OF_KHAINE") .. L": " .. classI)
      LabelSetText(window.."ClassK", RvRStats.GetMessage("SORCERER") .. L": " .. classJ)
      LabelSetText(window.."ClassL", RvRStats.GetMessage("BLACKGUARD") .. L": " .. classK)
   end      
   
end
